//
//  MLWeiboCellOriginalView.swift
//  MLSina20160624
//
//  Created by manmanlang on 16/6/28.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit
import SnapKit

private let OriginalMargin:CGFloat = 10
class MLWeiboCellOriginalView: UIView {
    
    var originalViewBottomConstraint: Constraint?
    var weiboData: MLWeiboData? {
        didSet {
            nameLabel.text = weiboData?.userInfo?.name
            // 用户头像
            headImageView.sd_setImageWithURL(NSURL(string: weiboData?.userInfo?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            // 微博内容
            contentLabel.text = weiboData?.text
            // 认证图片
            vipImageView.image = weiboData?.userInfo?.verifiedImage
            // 会员等级
            expiredImageView.image = weiboData?.userInfo?.mbrankImage
            
            sourceLabel.attributedText = weiboData?.sourceStr
            //getTimesStr(weiboData?.created_at ?? "") 微博发送时间
            timeLabel.text = weiboData?.createdStr
            
            originalViewBottomConstraint?.uninstall()
            
            if weiboData?.pic_urls?.count > 0 {
                pictureView.picture = weiboData?.pic_urls
                pictureView.hidden = false
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                    
                }
            }else {
                pictureView.hidden = true
                self.snp_makeConstraints { (make) -> Void in
                    originalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(StautsMargin).constraint
                }
            }
        }
    }
    
    ///计算显示的时间格式
    func getTimesStr(createAt: String) -> String {
        //Wed Jun 29 20:50:06 +0800 2016
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en_US")
        df.dateFormat = "EEE MM dd HH:mm:ss z yyyy"
        
        let wbDate = df.dateFromString(createAt)!
        // let wbDate = wbDate1.dateByAddingTimeInterval(-3600 * 24 * 30 * 365)
        
        let isThisYear = wbDateIsThisYear(wbDate)
        
        if isThisYear {
            let calender = NSCalendar.currentCalendar()
            if calender.isDateInToday(wbDate){
                let s = NSDate().timeIntervalSinceDate(wbDate)
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 60 * 60 {
                    return "\(Int(s / 60))分钟前"
                }else {
                    return "\(Int(s / 3600))小时前"
                }
                
                
            } else if calender.isDateInYesterday(wbDate){
                df.dateFormat = "昨天 HH:mm"
                return df.stringFromDate(wbDate)
            }else{
                df.dateFormat = "MM:dd HH:mm"
                return df.stringFromDate(wbDate)
            }
            
            
            
        }else {
            df.dateFormat = "yyyy年MM月dd日"
            return df.stringFromDate(wbDate)
        }
    }
    func wbDateIsThisYear(wbDate: NSDate) -> Bool{
        let df = NSDateFormatter()
        df.locale = NSLocale(localeIdentifier: "en_US")
        df.dateFormat = "yyyy"
        let wbStr = df.stringFromDate(wbDate)
        let currentStr = df.stringFromDate(NSDate())
        if wbStr == currentStr {
            return true
        }else {
            return false
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    ///微博配图
    private lazy var pictureView: MLPictureView = MLPictureView()
    ///头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default_big")
    ///用户昵称
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15)
    ///用户等级
    private lazy var expiredImageView: UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    ///发布微博
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    ///来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 10)
    //Vip
    private lazy var vipImageView: UIImageView = UIImageView(imgName: "avatar_grassroot")
    //微博内容
    private lazy var contentLabel:UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 14, maxWidth: UIScreen_Bounds_Width - OriginalMargin * 2)
    
    //MARK: - 设置视图
    private func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        /// 头像
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(OriginalMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        /// 昵称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(OriginalMargin)
        }
        /// 用户等级
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(OriginalMargin)
            make.centerY.equalTo(nameLabel)
        }
        /// 发布时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        ///来自
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.leading.equalTo(timeLabel.snp_trailing).offset(OriginalMargin)
        }
        ///认证
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        ///微博内容
        contentLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(headImageView.snp_leading)
            make.top.equalTo(headImageView.snp_bottom).offset(OriginalMargin)
        }
        ///配图
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(StautsMargin)
        }

        // 原创微博 动态计算微博高度
        self.snp_makeConstraints { (make) -> Void in
            originalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        
    }
}

extension MLWeiboCellOriginalView {
    ///// 会员等级 0-6
    //var mbrank: Int = 0
    func getMbrankImage(mbrank: Int) -> UIImage? {
        // 如果会员等级 0-6 使用一下
        if mbrank > 0 && mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(mbrank)")
        }else {
            // 其他使用默认
            return UIImage(named: "common_icon_membership_expired")
        }
        
    }
    
    // 通过后台返回来的数据来判断/// 认证类型，-1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
    func getVipImage(verified: Int) -> UIImage? {
        switch verified{
        case 0:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return nil
        }
    }
}
